package com.sky.controller.admin;



import cn.hutool.core.io.FileUtil;
import com.sky.constant.MessageConstant;
import com.sky.result.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.UUID;


@RestController
@RequestMapping("/admin/common")
@Slf4j
public class CommonController {

    /**
     * 文件上传接口
     *
     * @param file
     * @return
     */
    @PostMapping("/upload")
    public Result<String> upload(MultipartFile file) {
        log.info("文件上传:{}", file);
        String originalFilename = file.getOriginalFilename();
        try {
            if (originalFilename != null) {
                // 利用UUID构造新的文件名称
                String objectName = UUID.randomUUID().toString() + originalFilename;
                // 文件的请求路径
                String filePath = "C:\\study\\ssm\\sky-take-out\\files\\" + objectName;
                String returnImagePate = "http://127.0.0.1:8080/files/" + objectName;
                file.transferTo(new File(filePath));
                return Result.success(returnImagePate);
            } else {
                throw new IOException(MessageConstant.UPLOAD_FAILED);
            }
        } catch (IOException e) {
            e.printStackTrace();
            log.error("文件上传失败:{}", e);
        }
        return Result.error(MessageConstant.UPLOAD_FAILED);
    }










////
////    @Value("${ip}")
////    private String ip;
////
////    @Value("${server.port}")
////    private String port;
//
//    //    编写一个全局路径
//    private static final String ROOT_PATH = System.getProperty("user.dir") + "/files";
//
//    /**
//     * 文件上传
//     */
//    @PostMapping("/upload")
//    public Result upload(MultipartFile file) throws IOException {
//        String originalFilename = file.getOriginalFilename();//获取文件的原始文件夹
//
//        log.info("originalFilename:{}", originalFilename);
//
////        System.out.println("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" + originalFilename);
//        long flag = System.currentTimeMillis();//获取唯一标识
//        String fileName = flag + "_" + originalFilename;//文件名称
//        File finalFile = new File(ROOT_PATH + "/" + fileName);//最终存储到磁盘的对象
////        判断是否改文件目录
//        if (!finalFile.getParentFile().exists()) {
//            finalFile.getParentFile().mkdir();
//        }
//        file.transferTo(finalFile);
////        返回文件的url地址
//        String url = "http://" + "localhost" + ":" + 8080 + "/files/download?fileName=" + fileName;
////        返回文件的url地址
//        log.info("url:{}",url);
//        return Result.success(url);
//    }
//
//    /**
//     * 文件下载
//     */
//    @GetMapping("/download")
//    public void download(String fileName, HttpServletResponse response) throws IOException {
//        File file = new File(ROOT_PATH + "/" + fileName);
////        System.out.println("bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb" + fileName);
//        ServletOutputStream os = response.getOutputStream();
////        设置两个必须的头文件
//        response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8"));
//        response.setContentType("application/octet-stream");
//        FileUtil.writeToStream(file, os);
//        os.flush();
//        os.close();
//
//    }

//
//    /**
//     * 文件下载
//     */
//    @GetMapping("/download")
//    public void download(String fileName, HttpServletResponse response) throws IOException {
//        File file = new File(ROOT_PATH + "/" + fileName);
////        System.out.println("bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb" + fileName);
//        ServletOutputStream os = response.getOutputStream();
////        设置两个必须的头文件
//        response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8"));
//        response.setContentType("application/octet-stream");
//        FileUtil.writeToStream(file, os);
//        os.flush();
//        os.close();
//
//    }


}
